package top.mengchaob.fileserver.netty.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.mengchaob.fileserver.netty.api.FileServiceI;
import top.mengchaob.fileserver.netty.table.FileDo;
import top.mengchaob.fileserver.netty.table.FileLBDo;
import top.mengchaob.util.ResultBean;
import top.mengchaob.util.UtilTimeFormatter;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @title：
 * @author: mengchaob
 * @date: 2021年05月12日 16:23
 * @description:
 */
@Controller
@RequestMapping("fileController")
public class FileController {
    @Autowired
    FileServiceI fileServiceI;
    @RequestMapping("addFileLb")
    @ResponseBody
    ResultBean addFileLb(@RequestBody FileLBDo fileLBDo){
        fileLBDo.setLbId(UUID.randomUUID().toString());
        fileLBDo.setCreateTime(UtilTimeFormatter.getTimeToYMDHMS(new Date().getTime()));
        return fileServiceI.addFileLb(fileLBDo);
    }

    @RequestMapping("updateFileLb")
    @ResponseBody
    ResultBean updateFileLb(@RequestBody FileLBDo fileLBDo){
        fileLBDo.setCreateTime(UtilTimeFormatter.getTimeToYMDHMS(new Date().getTime()));
        return fileServiceI.updateFileLb(fileLBDo);
    }

    @RequestMapping("delFileLb")
    @ResponseBody
    ResultBean delFileLb(@RequestParam("lbCode")String lbCode){
        return fileServiceI.delFileLb(lbCode);
    }

    @RequestMapping("queryFileLb")
    @ResponseBody
    ResultBean queryFileLb(@RequestParam("lbCode")@Nullable String lbCode){

        return fileServiceI.queryFileLb(lbCode);
    }
    @RequestMapping("getFileLbAndFileInfoList")
    @ResponseBody
    public ResultBean getFileLbAndFileInfoList(){
        ResultBean rs = null;
        try {
            rs = fileServiceI.getFileLbAndFileInfoList();
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("系统异常！");
        }
        return rs;
    }
    @RequestMapping("addFile")
    @ResponseBody
    ResultBean addFile(HttpServletRequest request){
        FileDo fileDo = null;
        fileDo.setFileId(UUID.randomUUID().toString());
        fileDo.setCreateTime(UtilTimeFormatter.getTimeToYMDHMS(new Date().getTime()));
        return fileServiceI.addFile(fileDo);
    }

    @RequestMapping("updateFile")
    @ResponseBody
    ResultBean updateFile(@RequestBody FileDo fileDo){
        fileDo.setCreateTime(UtilTimeFormatter.getTimeToYMDHMS(new Date().getTime()));
        return fileServiceI.updateFile(fileDo);
    }

    @RequestMapping("delFile")
    @ResponseBody
    ResultBean delFile(@RequestParam("fileIds")String fileIds){
        String[] fileIds_ = fileIds.split(",");
        return fileServiceI.delFile(fileIds_);
    }

    @RequestMapping("queryFile")
    @ResponseBody
    ResultBean queryFile(@RequestParam("fileId")String fileId){
        return fileServiceI.queryFile(fileId);
    }

    @RequestMapping("queryFileByLbCode")
    @ResponseBody
    ResultBean queryFileByLbCode(@RequestParam("lbCode")@Nullable String lbCode){
        return fileServiceI.queryFileByLbCode(lbCode);
    }
}
